//
//  LiveCollectionViewCell.m
//  沙哈拉FM
//
//  Created by dllo on 15/11/3.
//  Copyright © 2015年 S1mpl3. All rights reserved.
//

#import "LiveCollectionViewCell.h"
#import "TwoToOne.h"
#import "HeadView.h"
#import "TelevisionCell.h"
#import "RecommendCell.h"
#import "RankingCell.h"
#import "ZBHistoryCell.h"
#import "ZBRecommandRadioListModel.h"
#import "ZBTopRadioListModel.h"
#import "ZBBPlayHistory.h"
#import "FitHeader.h"
#import "UIImage+GIF.h"


@implementation LiveCollectionViewCell

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self getData];
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        
        
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.minimumLineSpacing = 0;
        flowLayout.sectionInset = UIEdgeInsetsMake(10 * FITHeight, 10 * FITWidth, 10 * FITHeight, 10 * FITWidth);
        _collectionView  = [[ UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) collectionViewLayout:flowLayout];
        
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        
        [self addSubview:_collectionView];
        
        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"liveCell"];
        
        [_collectionView registerClass:[HeadView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head"];
        
        
        [_collectionView registerClass:[TelevisionCell class] forCellWithReuseIdentifier:@"television"];
        
        [_collectionView registerClass:[RecommendCell class] forCellWithReuseIdentifier:@"recommend"];
        
        [_collectionView registerClass:[RankingCell class] forCellWithReuseIdentifier:@"ranking"];
        [_collectionView registerClass:[ZBHistoryCell class] forCellWithReuseIdentifier:@"history"];
        
        _TelevisionArray = [NSArray arrayWithObjects:@"本地台",@"国家台", @"省市台", @"网络台", nil];
        
        _imageArray = [NSArray arrayWithObjects:@"zbblocal.png",@"zbbstars.png",@"zbbdianshitai.png", @"wangluo.png", nil];
        
        self.progressView = [[MBProgressHUD alloc]initWithView:self.contentView];
        [[[_progressView subviews]firstObject] removeFromSuperview];
        [[[_progressView subviews] objectAtIndex:1] removeFromSuperview];
        [_progressView setMode:0];
        [_progressView show:YES];
        NSString  *name = @"RED.gif";
        _progressView.color = [UIColor clearColor];
        NSString  *filePath = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:name ofType:nil];
        NSData  *imageData = [NSData dataWithContentsOfFile:filePath];
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(-70 * FITWidth, -80 * FITHeight, 140 * FITWidth, 100 * FITHeight)];
        imageView.layer.masksToBounds = YES;
        imageView.layer.cornerRadius = 10;
        imageView.opaque = NO;
        
        [[[[[_progressView subviews] lastObject] subviews] firstObject] removeFromSuperview];
        
        imageView.image = [UIImage sd_animatedGIFWithData:imageData];
        [[[_progressView subviews] lastObject] addSubview:imageView];
        [self.contentView addSubview:_progressView];
        
    }
    return self;
}

-(void)getData
{
    _dataRecommandSourceArray = [NSMutableArray array];
    self.dataTopSourceArray = [NSMutableArray array];
    [TwoToOne getDataByString:@"http://live.ximalaya.com/live-web/v1/getHomePageRadiosList?device=android"
                   BodyString:nil WithDataBlock:^(id data) {
                       
                       NSDictionary *dictionary = [data objectForKey:@"result"];
                       NSArray *RecommandRadioListArray = [dictionary objectForKey:@"recommandRadioList"];
                       _recommandRadioListArray = [dictionary objectForKey:@"recommandRadioList"];
                       NSArray *TopRadioListArray = [dictionary objectForKey:@"topRadioList"];
                       _topRadioListArray = [dictionary objectForKey:@"topRadioList"];
                       self.dataArray = [dictionary objectForKey:@"topRadioList"];
                       for (NSDictionary *dic in RecommandRadioListArray) {
                           
                           ZBRecommandRadioListModel *model = [[ZBRecommandRadioListModel alloc]init];
                           [model setValuesForKeysWithDictionary:dic];
                           [_dataRecommandSourceArray addObject:model];
                       }
                       
                       for (NSDictionary *dic1 in TopRadioListArray) {
                           ZBTopRadioListModel *model = [[ZBTopRadioListModel alloc]init];
                           [model setValuesForKeysWithDictionary:dic1];
                           [self.dataTopSourceArray addObject:dic1];
                       }
                       [_collectionView reloadData];
                       [_progressView removeFromSuperview];
                       
                   }];
    
    
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    switch (section) {
        case 0:
            return 4;
            break;
        case 1:
            return 3;
            break;
        case 2:
            return 3;
            break;
        case 3:
        {
            if (_playHistoryArray.count != 0) {
                
                return  _playHistoryArray.count;
            }
        }
            break;
        default:
            break;
    }
    return 0;
    
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 4;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 0) {
        TelevisionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"television" forIndexPath:indexPath];
        
        
        cell.imageView.image = [UIImage imageNamed:_imageArray[indexPath.row]];
        cell.label.text =  _TelevisionArray[indexPath.row];
        return cell;
    }
    else if (indexPath.section == 1)
    {
        
        RecommendCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"recommend" forIndexPath:indexPath];
        if (_dataRecommandSourceArray.count != 0) {
            cell.model = _dataRecommandSourceArray[indexPath.row];
        }
        return cell;
        
    }
    else if (indexPath.section == 2)
    {
        RankingCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ranking" forIndexPath:indexPath];
        
        if (_dataTopSourceArray.count != 0) {
            
        }
        
        cell.dictionary = _dataArray[indexPath.row];
        return cell;
    }
    else
    {
        ZBHistoryCell *cell =  [collectionView dequeueReusableCellWithReuseIdentifier:@"history" forIndexPath:indexPath];
        cell.dic = _playHistoryArray[indexPath.row];
        return cell;
        
    }
    
    
    
}
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    HeadView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head" forIndexPath:indexPath];
    switch (indexPath.section) {
        case 1:
            view.leftLabel.text = @"推荐电台";
            view.rightLabel.text = @"";
            view.moreImageView.hidden = YES;
            return view;
            
            break;
        case 2:
        {
            view.leftLabel.text = @"排行榜";
            view.rightLabel.text = @"更多";
            view.moreImageView.hidden = NO;
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
            [view.rightLabel addGestureRecognizer:tap];
            view.rightLabel.tag = 1000;
            
        }
            return view;
        case 3:
        {
            view.leftLabel.text = @"播放历史";
            view.rightLabel.text = @"更多";
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
            [view.rightLabel addGestureRecognizer:tap];
            view.rightLabel.tag = 1001;
            
        }
            return view;
        default:
            break;
    }
    return nil;
    
    
    
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {
        case 0:
            
            return CGSizeMake(80 * FITWidth, 120 * FITHeight);
            break;
        case 1:
            
            return CGSizeMake(110 * FITWidth, 120 * FITHeight);
            break;
        case 2:
            
            return CGSizeMake(375 * FITWidth, 90 * FITHeight);
            break;
        case 3:
            
            return CGSizeMake(375 * FITWidth, 90 * FITHeight);
            break;
        default:
            
            break;
    }
    return CGSizeMake(0, 0);
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section

{
    
    if (section) {
        return CGSizeMake(375 * FITWidth, 40 * FITHeight);
        
    }
    return CGSizeMake(0, 0);
}
-(void)tapAction:(UITapGestureRecognizer *)tap
{
    if (tap.view.tag == 1000) {
        
        [self.delegate PushToDetail:tap.view.tag];
        
    }
    else
    {
        if (_playHistoryArray.count != 0) {
            [self.delegate PushToDetail:tap.view.tag];
            
        }
        
    }
    
    
    
    
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        
        [self.delegate PushToDetail:indexPath.row];
    }
    if (indexPath.section == 1)
    {
        
        
        NSDictionary *dic = _recommandRadioListArray[indexPath.row];
        [self.delegate PushToRadioDetail:dic AndWith:YES];
        
        
        
      
        [[ZBBPlayHistory shareInstance] takeHistoryArrayFromFile:@"ppppp"];
        [[ZBBPlayHistory shareInstance] addDictionary:dic];
        [[ZBBPlayHistory shareInstance] WriteToFile:@"ppppp"];
        
    }
    else if(indexPath.section == 2)
    {
        NSDictionary *dic = _topRadioListArray[indexPath.row];
          [self.delegate PushToRadioDetail:dic AndWith:YES];
        [[ZBBPlayHistory shareInstance] takeHistoryArrayFromFile:@"ppppp"];
        [[ZBBPlayHistory shareInstance] addDictionary:dic];
        [[ZBBPlayHistory shareInstance] WriteToFile:@"ppppp"];
        
    }
    
    else  if(indexPath.section == 3)
    {
        NSDictionary *dic = _recommandRadioListArray[indexPath.row];
        [self.delegate PushToRadioDetail:dic AndWith:YES];
       
    }
}
-(void)addArray:(NSMutableArray *)array
{
    
    if (array.count >= 5) {
        [_playHistoryArray removeAllObjects];
        for (NSInteger i = (array.count - 5); i < array.count ; i++) {
            NSDictionary *dic = [array objectAtIndex:i];
            [_playHistoryArray addObject:dic];
            
        }
        
    }
    else
    {
        _playHistoryArray = array;
    }
    [_collectionView reloadData];
}
@end
